<?
	session_start(); // *** require session ***

	require 'pls.php';

	// open connection
	$hostname = "localhost";
	$user = "root";			// TODO update db username
	$password = "1234";		// TODO update db password
	$dbname = "test_login"; // TODO updaet db destination
	//
	$conn = mysql_connect($hostname, $user, $password) or die(mysql_error());
	mysql_select_db($dbname, $conn) or die(mysql_error());

	// operation
	
	switch($_GET['action']){
		case 'install':
			PLS::install();
			break;
		case 'uninstall':
			PLS::uninstall();
			break;
		case 'add_user':
			PLS::add_user($_POST['username'], $_POST['password']);
			break;
		case 'block_user':
			PLS::block_user($_GET['id']);
			break;
		case 'unblock_user':
			PLS::unblock_user($_GET['id']);
			break;
		case 'remove_user':
			PLS::remove_user($_GET['id']);
			break;
		case 'login':
			$result = PLS::login($_POST['username'], $_POST['password']);
			$login_message = $result['message'];
			break;
		case 'logout':
			PLS::logout();
			break;
	}

?>
<html>
	<head>
		<title>PLS - Php Login System</title>
		<style type='text/css'>
			input.install   { background-color: green; color: white; }
			input.uninstall { background-color: red;   color: white; }
		</style>
	</head>
	<body>
		<h1>
			<a href='index.php'>PLS - Php Login System</a>
			<? if(PLS::is_installed()){ ?>	<input type='button' class='uninstall' value='UNINSTALL' onclick="if(confirm('Uninstall ?')){ location.href='?action=uninstall' };" />
			<? } else { ?>					<input type='button' class='install' value='INSTALL' onclick="location.href='?action=install';" />
			<? } ?>
		</h1>
		<?
			if(PLS::is_installed()){ ?>
				<h3>Add User</h3>
				<form action='?action=add_user' method='post'>
					username <input type='text' name='username' /><br />
					password <input type='text' name='password' />
					<input type='submit' value='ADD' />
				</form>

				<h3>Login</h3>
				<form action='?action=login' method='post'>
					username <input type='text' name='username' /><br />
					password <input type='text' name='password' />
					<input type='submit' value='LOGIN' />
					<? if(PLS::is_login()){ ?>
						<input type='button' value='LOGOUT' onclick="location.href='?action=logout';" />
						<? }
						echo $login_message;
					?>
				</form>

				<h3>Session</h3>
				<ol>
					<? if(isset($_SESSION['PLS_USERNAME'])){ ?>
						<li>$_SESSION['PLS_USERNAME'] ==> <?=$_SESSION['PLS_USERNAME']?></li>
					<? } ?>
					<? if(isset($_SESSION['PLS_LAST_LOGIN'])){ ?>
						<li>$_SESSION['PLS_LAST_LOGIN'] ==> <?=$_SESSION['PLS_LAST_LOGIN']?></li>
					<? } ?>
				</ol>

				<h3>User Table</h3>
				<table border='1'>
					<tr>
						<td>id</td>
						<td>username</td>
						<td>digest</td>
						<td>salt</td>
						<td>pepper</td>
						<td>fail_login</td>
						<td>last_login</td>
						<td>status</td>
						<td>block</td>
						<td>remove</td>
					</tr>
					<?
						$result = mysql_query('select * from user;') or die(mysql_error());
						while($row = mysql_fetch_array($result)){ ?>
						<tr>
							<td><?=$row['id']?></td>
							<td><?=$row['username']?></td>
							<td><?=$row['digest']?></td>
							<td><?=$row['salt']?></td>
							<td><?=$row['pepper']?></td>
							<td><?=$row['fail_login']?></td>
							<td><?=$row['last_login']?></td>
							<td><?=$row['status']?></td>
							<td>
								<a href='?action=block_user&id=<?=$row['id']?>'>BLOCK</a> |
								<a href='?action=unblock_user&id=<?=$row['id']?>'>UNBLOCK</a>
							</td>
							<td><a href='?action=remove_user&id=<?=$row['id']?>'>REMOVE</a></td>
						</tr>
						<? }
					?>
				</table>
			<? }
		?>
	</body>
</html>
<?
	// close connection
	mysql_close($conn);
?>
